<?php

class Contact_us extends Controller {

	function Contact_us()
	{
		parent::Controller();
        $this->load->library("form_validation");    	
	}
	
	function index()
	{

        if($this->input->post("contact_submit") !== FALSE){

            if(validate_token("contact",$this->input->post("token",TRUE))){
                 
                if($this->_validate_contact_form()){

                    $this->load->helper('send_email');     

                    $template_file = EMAIL_TEMPLATES_FOLDER.'contact_us'.INSTANCE_FILE_SUFFIX.'.html';
                    $template_data = array(
                            "TITLE" => $this->input->post("title",TRUE),               
                            "FORENAME" => $this->input->post("forename",TRUE),
                            "SURNAME" => $this->input->post("surname",TRUE),
                            "COMPANYNAME" => $this->input->post("companyname",TRUE),
                            "CLIENTADDRESS" => $this->input->post("clientaddress",TRUE),
                            "CITY" => $this->input->post("city",TRUE),
                            "STATE_PROVINCE_COUNTY" => $this->input->post("state_province_county",TRUE),
                            "ZIP" => $this->input->post("zip",TRUE),
                            "COUNTRY" => $this->input->post("country",TRUE),
                            "PHONENUMBER" => $this->input->post("phonenumber",TRUE),
                            "MOBILE" => $this->input->post("mobile",TRUE), 
                            "EMAIL" => $this->input->post("email",TRUE),
                            "HOW_FIND_US" => $this->input->post("how_find_us",TRUE),
                            "BROCHURE" => $this->input->post("brochure",TRUE),
                            "PROJECT" => $this->input->post("project",TRUE),
                            "PRODUCTS" => $this->input->post("products",TRUE),
                            "PROJECTLOCATION" => $this->input->post("projectlocation",TRUE),
                            "PROJECTREFERENCE" => $this->input->post("projectreference",TRUE),
                            "PROJECTDETAILS" => $this->input->post("projectdetails",TRUE),
                            "FLOORAREA" => $this->input->post("floorarea",TRUE),
                            "WALLAREA" => $this->input->post("wallarea",TRUE),
                            "COMPLETIONTIMING" => $this->input->post("completiontiming",TRUE),
                            "ENQUIRYSTATUS" => $this->input->post("enquirystatus",TRUE),
                            "MESSAGE" => $this->input->post("message",TRUE),
                            "TIMESTAMP" => date('l jS \of F Y \a\t h:i:s A')
                            );

                    $sender_name = $this->input->post("forename",TRUE).' '.$this->input->post("surname",TRUE);

                    send_email($this->input->post("email",TRUE), 
                                $sender_name, 
                                EMAIL_RECEIVER, 
                                $this->lang->line('Contact_us_email_subject'), 
                                $template_file,
                                $template_data);
                    
                    $contact_submit_success = TRUE;         

                }

            }

        } 

        $data['user'] = $this->_set_default_values();

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Contact us"));  
        $this->load->view('website/default/header');  


        if (!isset($contact_submit_success)){

            //load page description
            $this->load->model('pageheadersmodel'); 
            $data["header"] = $this->pageheadersmodel->get_header('contact_us');
            $data['token'] = generate_token("contact",20);  
            $this->load->view('website/contact_us/contact_us', $data);               

        } else {
            $this->load->view('website/contact_us/contact_us_success');   
        }

        $this->load->view('website/default/footer');   
	}


    private function _validate_contact_form(){
        
        $config = array(
                        array(
                         'field'   => 'title',
                         'label'   => 'title',
                         'rules'   => 'trim|required|xss_clean|max_length[8]'
                        ),
                        array(
                         'field'   => 'forename',
                         'label'   => 'Forename',
                         'rules'   => 'trim|required|xss_clean|max_length[100]'
                        ),
                        array(
                         'field'   => 'surname',
                         'label'   => 'Surname',
                         'rules'   => 'trim|required|xss_clean|max_length[100]'
                        ),
                        array(
                         'field'   => 'companyname',
                         'label'   => 'Company Name',
                         'rules'   => 'trim|required|xss_clean|max_length[200]'
                        ),
                        array(
                         'field'   => 'clientaddress',
                         'label'   => 'Address',
                         'rules'   => 'trim|required|xss_clean|max_length[250]'
                        ),
                        array(
                         'field'   => 'city',
                         'label'   => 'City',
                         'rules'   => 'trim|xss_clean|alpha|required|max_length[100]'
                        ),
                        array(
                         'field'   => 'state_province_county',
                         'label'   => 'State/Province/County',
                         'rules'   => 'trim|xss_clean|alpha|required|max_length[100]'
                        ),
                        array(
                         'field'   => 'zip',
                         'label'   => 'ZIP/Postal Code',
                         'rules'   => 'trim|xss_clean|required|max_length[50]'
                        ),
                        array(
                         'field'   => 'country',
                         'label'   => 'Country',
                         'rules'   => 'trim|xss_clean|required'
                        ),
                        array(
                         'field'   => 'phonenumber',
                         'label'   => 'Phone Number',
                         'rules'   => 'trim|xss_clean|required|max_length[30]'
                        ),
                        array(
                         'field'   => 'mobile',
                         'label'   => 'Mobile Phone',
                         'rules'   => 'trim|xss_clean|max_length[30]'
                        ), 
                        array(
                         'field'   => 'email',
                         'label'   => 'Email Address',
                         'rules'   => 'trim|xss_clean|required|valid_email|callback_check_duplicated_email'
                        ),
                        array(
                         'field'   => 'how_find_us',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'brochure',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'project',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'products',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'projectlocation',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'projectreference',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'projectdetails',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'floorarea',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'wallarea',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'completiontiming',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'enquirystatus',
                         'label'   => '',
                         'rules'   => ''
                        ),
                        array(
                         'field'   => 'message',
                         'label'   => 'Other Requirements / General Message',
                         'rules'   => 'xss_clean|required|'
                        )       
                    );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

    private function _set_default_values(){

        if (!session_check() || ($this->session->userdata('user_id')=='vip')){
                            
            $user['Title_UsersDetails'] = "";
            $user['FirstName_UsersDetails'] = "";
            $user['LastName_UsersDetails'] = "";
            $user['CompanyName_UsersDetails'] = "";     
            $user['Address_UsersDetails'] = "";     
            $user['City_UsersDetails'] = "";     
            $user['Country_UsersDetails'] = "";     
            $user['State_UsersDetails'] = "";     
            $user['ZipCode_UsersDetails'] = "";  
            $user['DaytimePhone_UsersDetails'] = "";     
            $user['Mobile_UsersDetails'] = "";     
            $user['Email_UsersDetails'] = "";        

        } else {

            $this->load->model("usermodel"); 
            $user = $this->usermodel->get_user_details($this->session->userdata('user_id'));   

        }    

        return $user;

    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */